<?php

namespace App\Http\Controllers;

use App\Models\Common\ApiResponse;
use Illuminate\Http\Request;

class FileController extends Controller
{
    public function upload(Request $request): array
    {
        $disk = \Storage::disk('public');
        $file = $request->file('file');

        // 检测上传的文件是否合法
        if ($file->isValid()) {
            $extension = $file->getClientOriginalExtension();

            // uni-app 过来后经缀获取不到后缀
            if (!$extension) {
                // 通过 Image Type 获得
                $extension = explode('/', $file->getMimeType())[1];
            }
//            $path = $file->getClientOriginalName();
            $date = now()->format('Ymd');
            $key = str_replace('.', '', uniqid("/{$date}/", true)).'.'.$extension;
            if ($disk->exists($key)) {
                // 删除已存在文件
                $disk->delete($key);
            }

            $url = '';
            if ($disk->put($key, \file_get_contents($file->getRealPath()))) {
                if ($disk->exists($key)) {
                    $url = $request->getHttpHost().'/storage'.$key;
                }
            }

            return ApiResponse::return('', $url);
        }
        return ApiResponse::return('上传失败', '', ApiResponse::RESP_CODE_FAILURE);
    }

    public function uploadSignatureFile(Request $request): array
    {
        $file = $request->file('file');

        // 检测上传的文件是否合法
        if ($file->isValid()) {
            $extension = $file->getClientOriginalExtension();

            // uni-app 过来后经缀获取不到后缀
            if (!$extension) {
                // 通过 Image Type 获得
                $extension = explode('/', $file->getMimeType())[1];
            }
            if ('txt' !== $extension) {
                return ApiResponse::fail('文件格式错误');
            }
            if (0 === $file->getSize()) {
                return ApiResponse::fail('请勿上传空白文件');
            }
            if ($file->getSize() > 1024 * 2) {
                return ApiResponse::fail('签名文件过大,请检查后上传');
            }

            $uploadResult = $file->move(public_path(), $file->getClientOriginalName());
            if ($fileName = optional($uploadResult)->getFilename()) {
                return ApiResponse::return('', $fileName);
            }
        }
        return ApiResponse::return('上传失败', '', ApiResponse::RESP_CODE_FAILURE);
    }

    public function uploadCertFile(Request $request): array
    {
        $disk = \Storage::disk('public');
        $file = $request->file('file');

        // 检测上传的文件是否合法
        if ($file->isValid()) {
            $extension = $file->getClientOriginalExtension();

            // uni-app 过来后经缀获取不到后缀
            if (!$extension) {
                // 通过 Image Type 获得
                $extension = explode('/', $file->getMimeType())[1];
            }
            if (!in_array($extension, ['cer', 'pfx', 'pem'])) {
                return ApiResponse::fail('文件格式错误');
            }
            if (0 === $file->getSize()) {
                return ApiResponse::fail('请勿上传空白文件');
            }
            if ($file->getSize() > 1024 * 20) {
                return ApiResponse::fail('文件过大,请检查后上传');
            }

            $key = str_replace('.', '', uniqid("/certFile/", true)).'.'.$extension;
            if ($disk->exists($key)) {
                // 删除已存在文件
                $disk->delete($key);
            }

            $url = '';
            if ($disk->put($key, \file_get_contents($file->getRealPath()))) {
                if ($disk->exists($key)) {
                    $url = 'app/public'.$key;
                }
            }

            return ApiResponse::return('', $url);
        }
        return ApiResponse::return('上传失败', '', ApiResponse::RESP_CODE_FAILURE);
    }
}
